package com.ticket.sass.provider.dto.output.tenant;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ticket.sass.provider.dto.input.tenant.MatchColor;
import com.ticket.sass.provider.util.GsonUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @author ywb
 * @date 2024-03-26 18:10
 */
@Data
@ApiModel
public class ThemeConfigOutput {
    @ApiModelProperty("第一个主题")
    private MatchColor primary;
    
    @Override
    public String toString() {
        return GsonUtil.getInstance().toJson(this);
    }
    
    public ThemeConfigOutput() {
    }
    
    // 静态工厂方法
    public static ThemeConfigOutput fromJson(String jsonStr) {
        ThemeConfigOutput themeConfigOutput = new ThemeConfigOutput();
        
        if (StrUtil.isEmpty(jsonStr)) {
            return themeConfigOutput;
        }
        
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            ThemeConfigOutput tempOutput = objectMapper.readValue(jsonStr, ThemeConfigOutput.class);
            themeConfigOutput.primary = tempOutput.getPrimary();
        } catch (Exception e) {
            // 处理异常
            e.printStackTrace();
            themeConfigOutput.primary = null; // 设置默认值，避免异常情况下未初始化
        }
        
        return themeConfigOutput;
    }
}
